package com.sky.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import com.sky.dto.GetCategoriesDTO;
import com.sky.entity.Categories;
import com.sky.result.Result;
import com.sky.service.CategoriesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 分类管理相关接口
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminCategoryController {

    @Autowired
    private CategoriesService categoriesService;

    /**
     * 分页获取分类列表
     *
     * @param getCategoriesDTO 分页相关参数
     * @return 分类列表
     * @角色 SuperAdmin，ContentAdmin
     * @权限 CategoryManagementRelated
     */
    @SaCheckPermission(value = {"CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @PostMapping("/getCategory")
    public Result<Pager<Categories>> getCategory(@RequestBody GetCategoriesDTO getCategoriesDTO) {

        log.info("分页获取分类列表：{}", getCategoriesDTO);

        Pager<Categories> categoriesPager = categoriesService.getCateoriesPage(getCategoriesDTO);

        return Result.success(categoriesPager);

    }

    /**
     * 分类信息修改
     *
     * @param categories 分类信息
     * @return Result
     * @角色 SuperAdmin，ContentAdmin
     * @权限 CategoryManagementRelated
     */
    @SaCheckPermission(value = {"CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @PostMapping("/upCategory")
    public Result upCategory(@RequestBody Categories categories) {

        log.info("分类信息修改：{}", categories);

        categoriesService.upCategory(categories);

        return Result.success();

    }

    /**
     * 新增分类信息
     *
     * @param categories 分类信息
     * @return Result
     * @角色 SuperAdmin，ContentAdmin
     * @权限 CategoryManagementRelated
     */
    @SaCheckPermission(value = {"CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @PostMapping("/saveCategory")
    public Result saveCategory(@RequestBody Categories categories) {

        log.info("新增分类：{}", categories);

        categoriesService.saveCategory(categories);

        return Result.success();

    }

    /**
     * 删除分类
     *
     * @param categoryId 分类ID
     * @return Result
     * @角色 SuperAdmin，ContentAdmin
     * @权限 CategoryManagementRelated
     */
    @SaCheckPermission(value = {"CategoryManagementRelated"}, orRole = {"SuperAdmin", "ContentAdmin"})
    @DeleteMapping("/deleteCategory")
    public Result deleteCategory(Integer categoryId) {

        log.info("删除分类：{}", categoryId);

        categoriesService.deleteCategory(categoryId);

        return Result.success();

    }
} 